const HtmlWebpackPlugin = require('html-webpack-plugin')
const webpack = require('webpack')
const path = require('path')

const platfromEnv = process.env.PLATFORM_ENV
const {publicPath} = require(path.join(__dirname, './src/config'))

const config = {
    pack: {
        plugins: [
            new webpack.DefinePlugin({
                'process.env.PLATFORM_ENV': JSON.stringify(platfromEnv)
            }),
            new HtmlWebpackPlugin({
                template: path.join(__dirname, './src/index.html'),
                chunksSortMode: 'dependency',
                platfromEnv
            }),
        ],
    },
    server: {
        writeToDisk: platfromEnv === 'electron',
        port: 3000,
        routes: [
            '/:key'
        ],
        proxy: [
            {
                use: '/',
                proxyUrl: `https://${publicPath}`,
                filter: (req, res) => {
                    return (req.url.indexOf('/rest') == 0 || req.url.indexOf('/conf') == 0)
                },
                proxyReqPathResolver: (req, res) => {
                    return req.url
                }
            }
        ]
    }

}

if (process.env.NODE_ENV == 'production') {
    config.drop_console = true;
    config.pack.optimization = {
        splitChunks: {
            cacheGroups: {
                antdrc: {
                    chunks: 'initial',
                    name: 'antdrc',
                    test: /[\\/]node_modules[\\/](@ant-design.*|antd.*|rc-.*)[\\/]/,
                    enforce: true,
                }
            }
        }
    }
}

module.exports = config
